public  class Cbl_ProductAuTo {
    
    public static void ProductAuto(List<Product__c> list_new,Map<Id,Product__c> map_old)
    {
        Set<Id> set_Pro=new Set<Id>();
        Map<Id,ProductStructure__c> map_ps=new Map<Id,ProductStructure__c>();
        for(Product__c pr:list_new)
        {   
            //if(pr.ProductStructure__c!=null)
            if((map_old!=null && pr.ProductStructure__c!=null &&  pr.ProductStructure__c!=map_old.get(pr.Id).ProductStructure__c)
            ||(map_old==null && pr.ProductStructure__c!=null) )
            {
                set_Pro.add(pr.ProductStructure__c);
            }
            
        }
        List<ProductStructure__c> list_pt=[select id,Classify1__c,Classify2__c,Classify3__c
        from ProductStructure__c where id in:set_Pro];
        for(ProductStructure__c ps:list_pt)
        {
            map_ps.put(ps.Id,ps);
        }
        
        for(Product__c pr2:list_new)
        {
            if(map_ps.containsKey(pr2.ProductStructure__c))
            {
                if(map_ps.get(pr2.ProductStructure__c).Classify1__c!=null)
                {
                    
                    pr2.Category1__c=map_ps.get(pr2.ProductStructure__c).Classify1__c;
                }
                if(map_ps.get(pr2.ProductStructure__c).Classify2__c!=null)
                {
                    pr2.Category2__c=map_ps.get(pr2.ProductStructure__c).Classify2__c;
                }
                if(map_ps.get(pr2.ProductStructure__c).Classify3__c!=null)
                {
                    pr2.Category3__c=map_ps.get(pr2.ProductStructure__c).Classify3__c;
                    ////
                    //pr2.Category0__c=map_ps.get(pr2.ProductStructure__c).Classify3__c;
                }

                pr2.Category0__c=map_ps.get(pr2.ProductStructure__c).Classify3__c != null ? map_ps.get(pr2.ProductStructure__c).Classify3__c : map_ps.get(pr2.ProductStructure__c).Classify1__c ;

                 
            }
            // 为了便于仪器带试剂，自动做一个Category4 xudan
            
            if( pr2.Category2__c!=null && pr2.Category2__c.contains('-') )
            {
                pr2.Category4__c=pr2.Category2__c.split('-')[0];
            }
        }
        
    }
    
    
    //试剂信息汇总项目
    // public static void ProjectPro(List<Product__c> list_new)
    // {
    //     Map<String,ProductProject__c> map_pj=new Map<String,ProductProject__c>();
    //     for(Product__c pt:list_new)
    //     {
    //         if(pt.Category0__c=='试剂')
    //         {
    //             ProductProject__c pp=new ProductProject__c();
    //             pp.Name=pt.Name;
    //             pp.ExtID__c=pt.Name;
    //             pp.Category1__c=pt.Category1__c;
    //             pp.Category2__c=pt.Category2__c;
    //             pp.Category3__c=pt.Category3__c;
    //             pp.FullnameEng__c=pt.FullnameEng__c;
    //             pp.WarrantyDate__c=pt.WarrantyDate__c;
    //             pp.TestNumber__c=pt.TestNumber__c;
    //             pp.ProductStructure__c=pt.ProductStructure__c;
    //             //pp.Account__c=pt.Account__c;
    //             pp.Brand__c=pt.Brand__c;
    //             pp.BrandShort__c=pt.BrandShort__c;
    //             pp.SoftwareVersion__c=pt.SoftwareVersion__c;
    //             pp.Tax__c=pt.Tax__c;
    //             pp.Category4__c=pt.Category4__c;
    //             pp.ProductProject__c=pt.ProductProject__c;
    //             pp.Model__c=pt.Model__c;
    //             pp.InstSeries__c=pt.InstSeries__c;
    //             pp.ShortnameEng__c=pt.ShortnameEng__c;
    //             pp.FullnameEng__c=pt.FullnameEng__c;
    //             pp.FullnameChs__c=pt.FullnameChs__c;
    //             pp.Category0__c=pt.Category0__c;
    //             pp.WarrantyDate__c=pt.WarrantyDate__c;
    //             map_pj.put(pp.Name,pp);
    //         }
    //     } 
        
    //     upsert map_pj.values() ExtID__c; 
        
    //     for(Product__c ptNew:list_new)
    //     {
    //         if(ptNew.Category0__c=='试剂')
    //         {
    //             if(map_pj.containsKey(ptNew.Name))
    //             {
    //                 ptNew.ProductProject__c=map_pj.get(ptNew.Name).Id;
    //             }
    //         }
    //     }
    
    // }
}